package com.huluobo.high3demo.login;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm.baen.UserBeanResult;
import com.huluobo.mvvm.db.eneity.Money;
import com.huluobo.mvvm.db.eneity.UserInfo;
import com.huluobo.mvvm.model.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * 获取网络数据,并且给LiveData赋值
 * Created by LC on 2023/9/14.
 */
public class LoginRepo extends BaseRepo {

    /**
     * 参数形式请求
     *
     * @param username        用户名
     * @param password        密码
     * @param successLiveData 成功的回调
     * @param failedLiveData  失败的回调
     */
    public void login(String username, String password,
                      MutableLiveData<UserBeanResult.UserBean> successLiveData,
                      MutableLiveData<String> failedLiveData) {
        apiService.login(username, password).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBeanResult userBean) {
                        //成功的回调,给LiveData赋值
                        successLiveData.setValue(userBean.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        //失败的回调,同样给LiveData赋值
                        failedLiveData.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public void loginJson(RequestBody body, MutableLiveData<UserBeanResult.UserBean> success,
                          MutableLiveData<String> failed) {
        apiService.loginJson(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserBeanResult userBeanResult) {
                        success.setValue(userBeanResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 保存用户信息
     *
     * @param userInfo 用户的bean
     */
    public void saveUserToDatabase(UserInfo userInfo) {
        //让插入数据异步进行,之前写的RoomDemo中,增删改查都是在主线程当中运行,使用Completable 可以让该对象像Apiservice一样切换线程
        userInfoDao.insertUserInfo(userInfo).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }

    /**
     * 查询当前用户是否在数据库中
     *
     * @param username     用户名
     * @param querySuccess 成功的LiveData
     * @param queryError   失败的LiveData
     */
    public void queryUserFromDatabase(String username, MutableLiveData<UserInfo> querySuccess,
                                      MutableLiveData<String> queryError) {
        userInfoDao.queryUserInfo(username)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UserInfo userInfo) {
                        querySuccess.setValue(userInfo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        queryError.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void insertUserMoney(Money money) {
        moneyDao.insertUserMonty(money).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }
}
